package com.canteen.create.result;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

/**
 * @author bentley
 */
@Data
@Slf4j
public class ResponseResult implements Serializable {

    private boolean successful;
    private Object resultData;
    private String msg;
    private String type;
    private int code;
    private long count;

    /**
     * 默认返回成功
     */
    public ResponseResult() {
        this.successful = true;
        this.resultData = null;
        this.msg = "";
        this.type = RequestConstant.WRAPPED_RESULT_TYPE.INFO.getName();
        this.code = RequestConstant.WRAPPED_RESULT_CODE.CODE_O.getCode();
        this.count = 0;
    }

    public static ResponseResult successWrappedResult(String msg){
        return new ResponseResult(true, msg, RequestConstant.WRAPPED_RESULT_CODE.CODE_O.getCode(), null, 0, RequestConstant.WRAPPED_RESULT_TYPE.INFO.getName());
    }

    public static ResponseResult successWrappedResult(Object resultData){
        return new ResponseResult(true, "", RequestConstant.WRAPPED_RESULT_CODE.CODE_O.getCode(), resultData, 0, RequestConstant.WRAPPED_RESULT_TYPE.INFO.getName());
    }

    public static ResponseResult successWrappedResult(String msg, int code, long count){
        return new ResponseResult(true, msg, code, null, count, RequestConstant.WRAPPED_RESULT_TYPE.INFO.getName());
    }

    public static ResponseResult successWrappedResult(String msg, int code){
        return new ResponseResult(true, msg, code, null, 0, RequestConstant.WRAPPED_RESULT_TYPE.INFO.getName());
    }
    
    public static ResponseResult successWrappedResult(String msg, long count){
        return new ResponseResult(true, msg, RequestConstant.WRAPPED_RESULT_CODE.CODE_O.getCode(), null, count, RequestConstant.WRAPPED_RESULT_TYPE.INFO.getName());
    }

    public static ResponseResult successWrappedResult(String msg, Object resultData){
        return new ResponseResult(true, msg, RequestConstant.WRAPPED_RESULT_CODE.CODE_O.getCode(), resultData, 0, RequestConstant.WRAPPED_RESULT_TYPE.INFO.getName());
    }

    public static ResponseResult successWrappedResult(String msg, int code, Object resultData){
        return new ResponseResult(true, msg, code, resultData, 0, RequestConstant.WRAPPED_RESULT_TYPE.INFO.getName());
    }

    public static ResponseResult successWrappedResult(String msg, Object resultData, long count){
        return new ResponseResult(true, msg, RequestConstant.WRAPPED_RESULT_CODE.CODE_O.getCode(), resultData, count, RequestConstant.WRAPPED_RESULT_TYPE.INFO.getName());
    }

    public static ResponseResult failWrappedResult(String msg){
        return new ResponseResult(false, msg, RequestConstant.WRAPPED_RESULT_CODE.CODE_O.getCode(), null, 0, RequestConstant.WRAPPED_RESULT_TYPE.ERROR.getName());
    }

    public static ResponseResult failWrappedResult(String msg, int code){
        return new ResponseResult(false, msg, code, null, 0, RequestConstant.WRAPPED_RESULT_TYPE.ERROR.getName());
    }

    public static ResponseResult failWrappedResult(String msg, int code, Object resultData){
        return new ResponseResult(false, msg, code, resultData, 0, RequestConstant.WRAPPED_RESULT_TYPE.ERROR.getName());
    }

    public static ResponseResult warnWrappedResult(String msg){
        return new ResponseResult(false, msg, 0, null, 0, RequestConstant.WRAPPED_RESULT_TYPE.WARN.getName());
    }

    public ResponseResult(boolean successful, String msg, int code, Object resultData, long count, String type){
        this.successful = successful;
        this.msg = msg;
        this.code = code;
        this.resultData = resultData;
        this.count = count;
        this.type = type;

        if (!successful){
            log.error("ResponseResult failed: " + msg);
        }
    }

}
